﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SmartSpace.Orgs
{
    public partial class OrgsService : IOrgsService
    {
        public ServiceResult SaveType(NodeType type)
        {
            using (OrgsDbContext context = new OrgsDbContext())
            {
                if (string.IsNullOrEmpty(context.Set<NodeType>().Where(p => p.Code == type.Code).Select(p => p.Code).SingleOrDefault()))
                {
                    context.Set<NodeType>().Add(type);
                }
                else
                {
                    context.Entry(type).State = System.Data.Entity.EntityState.Modified;
                }

                if (context.SaveChanges() > 0)
                {
                    return new ServiceResult(true);
                }

                return new ServiceResult(false);
            }
        }

        public ServiceResult DeleteType(NodeType type)
        {
            using (OrgsDbContext context = new OrgsDbContext())
            {
                if (string.IsNullOrEmpty(context.Set<NodeType>().Where(p => p.Code == type.Code).Select(p => p.Code).SingleOrDefault()))
                {
                    return new ServiceResult(true,"该节点类型不存在于系统中");
                }
                else
                {
                    context.Entry(type).State = System.Data.Entity.EntityState.Deleted;
                }

                if (context.SaveChanges() > 0)
                {
                    return new ServiceResult(true);
                }

                return new ServiceResult(false);
            }
        }

        public bool DeleteType(string code)
        {
            using (OrgsDbContext context = new OrgsDbContext())
            {
                NodeType type = context.Set<NodeType>().Where(p => p.Code == code).SingleOrDefault();
                if (type == null)
                {
                    return true;
                }
                else
                {
                    context.Entry(type).State = System.Data.Entity.EntityState.Deleted;
                }

                if (context.SaveChanges() > 0)
                {
                    return true;
                }

                return false;
            }
        }

        public bool IncludeSubType(Guid parentTypeId, Guid subTypeId)
        {
            using (OrgsDbContext context = new OrgsDbContext())
            {
                if (context.Set<TypeRelation>().Where(p => p.ParentTypeId == parentTypeId && p.SubTypeId == subTypeId).Select(p => p.Id).SingleOrDefault() == default(Guid))
                {
                    return true;
                }
                else
                {
                    TypeRelation relation = new TypeRelation();
                    relation.Id = Guid.NewGuid();
                    relation.ParentTypeId = parentTypeId;
                    relation.SubTypeId = subTypeId;
                    context.Set<TypeRelation>().Add(relation);
                }

                if (context.SaveChanges() > 0)
                {
                    return true;
                }

                return false;
            }
        }

        public bool RemoveSubType(Guid parentTypeId, Guid subTypeId)
        {
            using (OrgsDbContext context = new OrgsDbContext())
            {
                TypeRelation relation = context.Set<TypeRelation>().Where(p => p.ParentTypeId == parentTypeId && p.SubTypeId == subTypeId).SingleOrDefault();
                if(relation == null)
                {
                    return true;
                }
                else
                {
                    context.Entry(relation).State = System.Data.Entity.EntityState.Deleted;
                }

                if (context.SaveChanges() > 0)
                {
                    return true;
                }

                return false;
            }
        }

        public NodeType GetType(string code)
        {
            using (OrgsDbContext context = new OrgsDbContext())
            {
                return context.Set<NodeType>().Where(p => p.Code == code).SingleOrDefault();
            }
        }

        public ListResult<NodeType> GetTypes()
        {
            using (OrgsDbContext context = new OrgsDbContext())
            {
                List<NodeType> types = context.Set<NodeType>().ToList();
                return new ListResult<NodeType>(types);
            }
        }

        public ListResult<NodeType> GetSubTypes(Guid typeId)
        {
            using (OrgsDbContext context = new OrgsDbContext())
            {
                List<NodeType> subs = context.Set<NodeType>()
                    .Join(context.Set<TypeRelation>().Where(p => p.ParentTypeId == typeId),
                        ot => ot.Id,
                        otr => otr.SubTypeId,
                        (ot, otr) => ot)
                    .ToList();

                return new ListResult<NodeType>(subs);
            }
        }
    }
}
